//
//  NovelViewController.m
//  shusheng
//
//  Created by rimi on 16/7/9.
//  Copyright © 2016年 yuying. All rights reserved.
//

#import "NovelViewController.h"
#import "NewCreatOneNovelViewController.h"
#import "NewCreatOneChapter.h"
#import "NewEditorWord.h"

@interface NovelViewController ()<UITableViewDataSource,UITableViewDelegate,UIViewControllerTransitioningDelegate>


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,assign) BOOL isNovelList;//


@property (nonatomic,strong) NSMutableArray *dataArr;
//@property (nonatomic,strong) NSMutableDictionary *dataDic;
@property (nonatomic,strong) EssayViewController *essayVC;

@end

@implementation NovelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isNovelList = YES;
    self.transitioningDelegate = self;
    self.modalPresentationStyle = UIModalPresentationCustom;
    [self loading];
    [self addRefreshHeader];
    
    if (_dataArr.count == 0) {
        [MBProgressHUD showSuccess:@"暂无数据，可添加"];
        [_tableView.mj_header endRefreshing];
    }

}

- (void)addRefreshHeader{
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            for (int i = 0; i < _dataArr.count; i ++) {
                [_dataArr[i] removeAllObjects];
            }
            [self loading];
            
     
        [_tableView.mj_header endRefreshing];
    }];
}
#pragma make -- UIViewControllerTransitioningDelegate --
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    
    return [AnimatedTransitioning transitionWithTransitionType:CircleSpreadTransitionTypePresent];
}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return [AnimatedTransitioning transitionWithTransitionType:CircleSpreadTransitionTypeDismiss];
}
#pragma mark -- loadingdata --
#pragma mark --
- (void)loading{
    
    //获取小说列表~本地，服务器。
    //查找plist文件，存在，取数据更新tableview、不存在，连接网络服务器，取数据更新tableview
    
    _dataArr = [NewCreatOneNovelViewController read];//获取
        NSLog(@"更新tableview%lu",(unsigned long)[_dataArr count]);
        //更新tableview
        
        [_tableView reloadData];/**< ~~~~~~~~~~~~~~~~~~~~~~&***重要**& */
    

  

}

#pragma mark -- UITableViewDataSource,UITableViewDelegate --
#pragma mark --

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    [self loading];
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NovelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NovelTableViewCell"];
    if (!cell) {
        cell = [[NovelTableViewCell alloc]init];
    }
    
    cell.nameLabel.text = _dataArr[indexPath.row][@"name"];
    cell.timeLabel.text = _dataArr[indexPath.row][@"time"];
    cell.synopsisLabel.text = _dataArr[indexPath.row][@"synopsis"];
    cell.imageName.text = _dataArr[indexPath.row][@"name"];
    cell.pic.image = [UIImage imageNamed:@"f.png"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"%ld",(long)indexPath.row);
    
    
    _essayVC = [self.storyboard instantiateViewControllerWithIdentifier:@"EssayViewController"];//章节vc
    _essayVC.isNovelList = YES;
    _essayVC.novelNameString = _dataArr[indexPath.row][@"name"];
    _essayVC.novelId = _dataArr[indexPath.row][@"novelId"];
//   
    [self addChildViewController:_essayVC];
    [self.view addSubview:_essayVC.view];

//    [self presentViewController:_essayVC animated:YES completion:nil];
   
}

#pragma mark -- UITableViewDelegate2 --
#pragma mark --
//是否允许编辑~默认yes
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//设置编辑样式~默认删除样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}


//修改删除确认按钮的标题
- (NSString * )tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";//~~默认是Delete;
}
//提交编辑样式~~~必须要实现的
//EditingStyle~~通过该参数来判断提交删除还是提交插入；
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除小说
        //得到所有章节
        //删对应txt
        //删所有章节
        //删小说

        NSArray *chaptersArr = [NewCreatOneChapter readOfNovelId:_dataArr[indexPath.row][@"novelId"]];
        NSLog(@"将要删除的章节%@",chaptersArr);
        for (int i = 0; i < chaptersArr.count; i ++) {
            [NewEditorWord deleteEditor:chaptersArr[i][@"word"]];
            NSLog(@"删对应txt%@其内容%@",chaptersArr[i][@"word"],[NewEditorWord readFileName:chaptersArr[i][@"word"]]);
            
        }

        //2删该小说的所有章节列表
        NSArray *chapters = [NewCreatOneChapter deleteAllChaptersOfNovelId:_dataArr[indexPath.row][@"novelId"] userId:_dataArr[indexPath.row][@"userId"]];
        NSLog(@"删除的章节%@",chapters);
        //删txt
        
        //1删除列表源
//        _dataArr = [NewCreatOneNovelViewController deleteNovelOfNovelId:_dataArr[indexPath.row][@"novelId"]];
        _dataArr = [NewCreatOneNovelViewController deleteNovelOfNovelId:_dataArr[indexPath.row][@"novelId"] userId:_dataArr[indexPath.row][@"userId"]];
        //删除单元行
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

//移动~允许移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;//默认为NO
}
//移动操作
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    
    //数据源
    NSDictionary * dataCell = _dataArr[sourceIndexPath.row];
    NSMutableArray *marr = [NSMutableArray arrayWithArray:_dataArr];
    [marr removeObject:dataCell];
    [marr insertObject:sourceIndexPath atIndex:destinationIndexPath.row];
    _dataArr = [NSMutableArray arrayWithArray:marr];
    
    //单元行
    [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
    
}

#pragma mark -- BtnAction --
#pragma mark --

- (IBAction)addNovelBtn:(UIButton *)sender {
    //添加元素
    NewCreatOneNovelViewController *newOne = [self.storyboard instantiateViewControllerWithIdentifier:@"NewCreatOneNovelViewController"];
    //新建小说的参数自定。
    [self addChildViewController:newOne];
    [self.view addSubview:newOne.view];
    
}

- (IBAction)responBtn:(UIButton *)sender {
    NSLog(@"返回");
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
